C# 用ini檔讀寫暫存資料


首先在最外層給他一個宣告

[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern long WritePrivateProfileString(string section,
string key, string val, string filePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern int GetPrivateProfileString(string section,
string key, string def, StringBuilder retVal,
int size, string filePath);

string iniPath = "Setup.ini";
string StartPath = Application.ExecutablePath.ToString();

在Form_Load指定ini檔名

iniPath = SetPath(iniPath);
iniGetSet();

接下來就是讀取跟寫入
中文的地方都是照自己要的填入

//ini
private string SetPath(string iniLogPath)
{
    string path = "";
    string[] arrPath = StartPath.Split('\\');
    for (int i = 0; i < arrPath.Length - 1; i++)
    {
        path += arrPath[i];
        path += "\\";
    }
    path += iniLogPath;

    return path;
}

void iniGetSet()
{
    int size = 3000;//temp file source size checkDay txtDay
    StringBuilder temp = new StringBuilder(size); //temp file source

    GetPrivateProfileString("類別名", "要讀的變數", "", temp, size, iniPath);
    要讀的變數 = temp.ToString();
}

public static void iniWriteSet()
{
    int size = 3000;//temp file source size checkDay txtDay
    StringBuilder temp = new StringBuilder(size); //temp file source

    WritePrivateProfileString("類別名", "寫入的變數", 寫入的變數值, iniPath);
}

ini檔裡就會長這樣

[類別名]
變數1=變數值1
變數2=變數值2
變數3=變數值3
#C# #Winform







你可能感興趣的文章

每日心得筆記 2020-06-20(六)

每日心得筆記 2020-06-20(六)

javascript 相關名詞

javascript 相關名詞

變成rule的形狀(4) - ESLint + Prettier + Stylelint 問題集

變成rule的形狀(4) - ESLint + Prettier + Stylelint 問題集






留言討論